package com.example.zjx8969.myapplication;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.transition.Slide;
import android.util.Log;
import android.view.Gravity;
import android.view.animation.LinearInterpolator;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import java.lang.ref.WeakReference;

/**
 * Created by zjx8969 on 2018/2/7.
 */

public class BaseActivity extends AppCompatActivity {
    private CommonHandler handler;
    BaseLifeCycleObserver observer;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        observer = new BaseLifeCycleObserver(this);
        getLifecycle().addObserver(observer);

        //Transition页面切换需要同样的background，实际开发中要求太苛刻。
        Slide slide = new Slide(Gravity.START);
        slide.setInterpolator(new LinearInterpolator());
        slide.excludeTarget(android.R.id.statusBarBackground, true);
        slide.excludeTarget(android.R.id.navigationBarBackground, true);
        Slide slideEnter = new Slide(Gravity.END);
        slideEnter.setInterpolator(new LinearInterpolator());
        slideEnter.excludeTarget(android.R.id.statusBarBackground, true);
        slideEnter.excludeTarget(android.R.id.navigationBarBackground, true);
/*        Transition transitionEnter = slideEnter.setDuration(200);
        Transition transitionLeave = slide.setDuration(200);*/
        getWindow().setEnterTransition(slideEnter);
        getWindow().setReturnTransition(slideEnter);

        getWindow().setReenterTransition(slide);
        getWindow().setExitTransition(slide);
        getWindow().setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(this, R.color.colorAccent)));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @Override
    protected void onResume() {
        super.onResume();
//        Log.e("tag", getClass().getSimpleName() + ">onResume");

    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e("tag", getClass().getSimpleName() + ">onStop");
    }

    public CommonHandler getHandler() {
        if (handler == null) {
            handler = new CommonHandler(this);
        }
        return handler;
    }

    public static class CommonHandler extends Handler {
        WeakReference<BaseActivity> mReference;

        public CommonHandler(BaseActivity activity) {
            super(Looper.getMainLooper());
            this.mReference = new WeakReference<>(activity);
        }


        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            BaseActivity baseActivity = mReference.get();
            if (baseActivity == null) {
                Log.e(getClass().getName(), "baseActivity==null");
            }
            if (baseActivity == null || baseActivity.isFinishing()) return;
            if (baseActivity.isDestroyed()) return;

            baseActivity.handleHandlerMessage(msg);
        }
    }

    public void handleHandlerMessage(Message msg) {

    }

}
